<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="内容">
                <a-input v-model="queryParam.content" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="用户名">
                <a-input v-model="queryParam.username" placeholder=""/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="状态">
                  <!-- 1:待受理2:已受理3:已完成 -->
                  <a-select v-model="queryParam.status" placeholder="请选择">
                    <a-select-option value="1">待受理</a-select-option>
                    <a-select-option value="2">已受理</a-select-option>
                    <a-select-option value="3">已完成</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="提交时间">
                  <a-range-picker style="width: 100%" v-model="queryParam.submit_time" />
                  <!-- <a-date-picker v-model="queryParam.date" style="width: 100%" placeholder="请输入更新日期"/> -->
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="false"
        showPagination="auto"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
        </span>
        <span slot="content" slot-scope="text">
          <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
        </span>

        <span slot="submit_time" slot-scope="text">
          {{ new Date(text * 1000).toLocaleDateString() }}
        </span>

        <span slot="location" slot-scope="text">
          <a href="javascript:;" @click="initMap(text)">定位</a>
        </span>

        <span slot="img" slot-scope="text">
          <viewer>
            <div class="table-img-wrap">
              <div v-for="itemImg in text" :key="itemImg.id">
                <!-- <a :href="itemImg.host + itemImg.img_url" target="_blank"> -->
                <img :src="itemImg.host + itemImg.img_url" />
                <!-- </a> -->
              </div>
            </div>
          </viewer>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <!-- <a @click="handleEdit(record)">修改状态</a> -->
            <a-dropdown>
              <a-menu slot="overlay">
                <!-- 1:待受理2:已受理3:已完成 -->
                <a-menu-item><a @click="onChangeStatus(record,1)">待受理</a></a-menu-item>
                <a-menu-item><a @click="onChangeStatus(record,2)">已受理</a></a-menu-item>
                <a-menu-item><a @click="onChangeStatus(record,3)">已完成</a></a-menu-item>
              </a-menu>
              <a>修改状态<a-icon type="down"/></a>
            </a-dropdown>

            <a-divider type="vertical" />
            <a @click="handleDel(record)">删除</a>
          </template>
        </span>
      </s-table>
      <!-- 地图 -->
      <div class="tx-map-wrap" v-show="mapVisible">
        <div style="color: white;display: flex;justify-content: flex-end;">
          <div class="map-close-icon" @click="onCloseMap">
            <a-icon type="close" />
          </div>
        </div>
        <div class="tx-map-main">
          <div style="flex: 1;">
            <div class="map-container" id="mapcontainer"></div>
          </div>
        </div>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
// import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { query, changeStatus, del } from '@/api/report'

const columns = [
  {
    title: '#',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '内容',
    dataIndex: 'content',
    scopedSlots: { customRender: 'content' }
  },
  {
    title: '用户名',
    dataIndex: 'user.username'
  },
  {
    title: '提交时间',
    dataIndex: 'submit_time',
    scopedSlots: { customRender: 'submit_time' }
  },
  {
    title: '图片',
    dataIndex: 'img',
    scopedSlots: { customRender: 'img' }
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '位置',
    dataIndex: 'location',
    scopedSlots: { customRender: 'location' }

  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]
// 1:待受理2:已受理3:已完成
const statusMap = {
  1: {
    status: 'default',
    text: '待受理'
  },
  2: {
    status: 'processing',
    text: '已受理'
  },
  3: {
    status: 'success',
    text: '已完成'
  }
}

export default {
  name: 'Report',
  components: {
    STable,
    Ellipsis
    // CreateForm
    // StepByStepModal
  },
  data () {
    this.columns = columns
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        return query(requestParameters).then(res => {
          return res.result
        }).catch(errors => {
          console.log(errors)
        })
      },
      txMap: null,
      mapVisible: false
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  created () {
  },
  computed: {
  },
  methods: {
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    onChangeStatus (record, status) {
      console.log(record, status)
      const map = {
        1: '待受理', 2: '已受理', 3: '已完成'
      }
      changeStatus({ id: record.id, status }).then(res => {
        console.log(res)
        // 刷新表格
        this.$refs.table.refresh()
        this.$message.success(`已修改为${map[status]}`)
      }).catch(errors => {
        console.log(errors)
      })
    },
    handleDel (record) {
      const _that = this
      this.$confirm({
        title: '请确认要删除报事？',
        content: h => <div style="color:red;">正在删除“{ record.content }”</div>,
        icon: h => <a-icon style="color:red;" type="delete" />,
        onOk () {
          del({ id: record.id }).then(res => {
            // console.log(res)
            _that.$message.success('删除成功')
            // 刷新表格
            _that.$refs.table.refresh()
          }).catch(errors => {
            console.log(errors)
          })
        },
        onCancel () {
          console.log('Cancel')
        }
      })
    },
    initMap (data) {
      console.log(data)
      this.mapVisible = true
      const { latitude, longitude } = data
      /* eslint-disable */
      const center = new TMap.LatLng(latitude, longitude)

      this.$nextTick(function () {
        // DOM 现在更新了
        // `this` 绑定到当前实例

        // var centerHeight = new TMap.LatLng(39.98210863924864, 116.31310899739151, 134)
        if (this.txMap === null) {
          this.txMap = new TMap.Map('mapcontainer', {
            // zoom: 17, // 设置地图缩放
            // center: new TMap.LatLng(latitude, longitude), // 设置地图中心点坐标
            pitch: 0, // 俯仰度
            rotation: 0 // 旋转角度
          })
        }

        this.txMap.setCenter(new TMap.LatLng(latitude, longitude))
        this.txMap.setZoom(17)
        

        // MultiMarker文档地址：https://lbs.qq.com/webApi/javascriptGL/glDoc/glDocMarker
        const marker = new TMap.MultiMarker({
          map: this.txMap,
          styles: {
            // 点标记样式
            marker: new TMap.MarkerStyle({
              width: 20, // 样式宽
              height: 30, // 样式高
              anchor: { x: 10, y: 30 } // 描点位置
            })
          },
          geometries: [
            // 点标记数据数组
            {
              // 标记位置(纬度，经度，高度)
              position: center,
              id: 'marker'
            }
          ]
        })
        
      })
      
    },
    onCloseMap () {
      this.mapVisible = false
    }
  }
}
</script>

<style scoped>
.table-img-wrap {
  display: flex;
  width: 200px;
  overflow: auto;
}
.table-img-wrap > div {
  margin: 0 10px 0 0;
}
.table-img-wrap img {
  width: 40px;
  height: 40px;
}
.tx-map-wrap {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 9999;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  flex-direction: column;
  padding: 20px;
  box-sizing: border-box;
}

.map-close-icon {
  /* margin: 20px 20px 0 0; */
  background-color: #515151;
  border-radius: 50%;
  padding: 10px;
  width: 60px;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 30px;
  cursor: pointer;
}

.tx-map-main {
  padding: 40px;
  /* background-color: blue; */
  flex: 1;
  display: flex;
  flex-direction: column;
}
.map-container {
  width: 100%;
  height: 100%;
}
</style>
